<?php

namespace App\V3\V3_2_7\Controllers;
use App\Http\Controllers\Controller;
use App\V3\V3_2_7\Models\PddSellerCodeListModel;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Response;
use Justmd5\PinDuoDuo\PinDuoDuo;

use function AlibabaCloud\Client\json;
use Illuminate\Http\Request;

Class PingDuoDuoOpenPlatformController extends BaseController {
    protected $pinDuoDuo;
    protected $token;
    public function __construct(Request $request){
        parent::__construct($request);
        $config = [
            'client_id'    => 'd75a149f595347679b0d00b83671edd7',
            'client_secret' => '2fbd0b4ce8b82ff53cdaaf7e0231f6abc11cd624',
            'debug'              => true,
            'member_type'        => 'MERCHANT',//用户角色 ：MERCHANT(商家授权),H5(移动端),多多客(JINBAO),
            'redirect_uri'       => 'https://'.$_SERVER['SERVER_NAME'].'/'.$_SERVER['REQUEST_URI'],
            'log'                => [
                'name'       => 'pinduoduo',
                'file'       => __DIR__ . '/pinduoduo.log',
                'level'      => 'debug',
                'permission' => 0777,
            ],
        ];

        $this->pinDuoDuo = new PinDuoDuo($config);
        if(Redis::exists('pdd_seller_access_token')) {
            $tokenInfo = json_decode(Redis::get('pdd_seller_access_token'),true);
            $tokenInfo = $this->pinDuoDuo->pre_auth->refreshToken($tokenInfo['refresh_token']);
            $this->pinDuoDuo->access_token->setToken($tokenInfo['access_token']);
        }
    }

    /**
     * 获取商家授权地址
     */
    public function pddGetSellerUrl(Request $request){
            $url = $this->pinDuoDuo->pre_auth->authorizationUrl($this->getMainUserId());
            return response()->json(['code'=>0,'msg'=>'success','data'=>['url'=>$url]]);
    }

    /**
     *获取token
     * Author: zlf
     * Created at 2023/7/28 10:53
     * @param Request $request
     * @return void
     */
    public function getAccessToken(Request $request){
        $json=$this->pinDuoDuo->pre_auth->getAccessToken('d7914e3580604510a7f41330b3a2547cb36c0ed6');

        return response()->json(['code'=>0,'msg'=>'success','data'=>$json]);
    }
    /**
     * 快递公司查看接口
     * @param Request $request
     */
    public function pddLogisticsCompaniesGet(Request $request){
        $res = $this->pinDuoDuo->auth_api->request('pdd.logistics.companies.get');
        return response()->json(['code'=>1,'msg'=>'success','data'=>$res]);
    }

    /**
     * 获取所有标准电子面单模板
     * @param Request $request
     */
    public function pddCloudprintStdtemplatesGet(Request $request){
        $params = array(
            'wp_code'=>'STO'
        );
        $info = $this->pinDuoDuo->auth_api->request('pdd.cloudprint.stdtemplates.get',$params);
        $info= json_encode($info);
        var_dump($info);die;
    }

    /**
     * 商品运费模版接口
     * @param Request $request
     */
    public function pddGoodsLogisticsTemplateGet(Request $request){
        $res = $this->pinDuoDuo->auth_api->request('pdd.goods.logistics.template.get');
        return response()->json(['code'=>1,'msg'=>'success','data'=>$res]);
    }

    /**
     * 商品地区/国家接口
     * @param Request $request
     */
    public function pddGoodsCountryGet(Request $request){
        $res = $this->pinDuoDuo->auth_api->request('pdd.goods.country.get');
        return response()->json(['code'=>1,'msg'=>'success','data'=>$res]);
    }

    /**
     * 商品新增接口
     * @param Request $request
     */
    public function pddGoodsAdd(Request $request){
        $params = array(
            'carousel_gallery'=>['https://img.smerp.top/uploads/2023/06/30/UoTjCjTFzV2QQq0xcF7DsGRAZcqtj7R00mohGs9z.png'],
            'cat_id'=>44771,
            'cost_template_id'=>10000,
            'country_id'=>1,
            'detail_gallery'=>['https://img.smerp.top/uploads/2023/06/30/UoTjCjTFzV2QQq0xcF7DsGRAZcqtj7R00mohGs9z.png'],
            'goods_desc'=>'ceshi',
            'goods_name'=>'ceshi',
            'goods_type'=>1,
            'is_folt'=>true,
            'is_pre_sale'=>true,
            'is_refundable'=>true,
            'market_price'=>1,
            'second_hand'=>false,
            'sku_list'=>'[{ "is_onsale": 1, "limit_quantity": 999, "price": "2200", "weight": 1000, "multi_price": "1900", "thumb_url": "http://t06img.yangkeduo.com/images/2018-04-15/ced035033b5d40b589140af882621c03.jpg", "out_sku_sn": "L", "quantity": 100, "spec_id_list": "[25]", "oversea_sku": { "measurement_code": "计量单位编码", "taxation": 1, "specifications": "规格" } }]'
          /*   'shipment_limit_second'=>,
            'shipment_limit_second'=>,
            'shipment_limit_second'=>,
            'shipment_limit_second'=>,
            'shipment_limit_second'=>,
            'shipment_limit_second'=>,
            'shipment_limit_second'=>,
            'shipment_limit_second'=>,
            'shipment_limit_second'=>,
            'shipment_limit_second'=>,
             */
        );
        $res = $this->pinDuoDuo->auth_api->request('pdd.goods.add',$params);
        echo "<pre>";
        var_dump($res);die;
    }

    /**
     * 订单列表查询接口（根据成交时间）
     * @param Request $request
     */
    public function pddOrderListGet(Request $request){
        $params = array(
            'end_confirm_at'=>time(),
            'order_status'=>5,
            'page'=>1,
            'page_size'=>10,
            'refund_status'=>5,
            'start_confirm_at'=>1690738785,
            'trade_type'=>0,
            'use_has_next'=>true,

        );

        $res = $this->pinDuoDuo->auth_api->request('pdd.order.list.get',$params);
        return response()->json(['code'=>0,'msg'=>'success','data'=>$res]);
    }

    /**
     * 查询订单详情接口
     * @param Request $request
     */
    public function pddOrderInformationGet(Request $request){
        $params = array(
            'order_sn'=>'230731-123422709420206'
        );

        $res = $this->pinDuoDuo->auth_api->request('pdd.order.information.get',$params);
        return response()->json(['code'=>0,'msg'=>'success','data'=>$res]);
    }

    /**
     * erp打单信息同步
     * @param Request $request
     */
    public function pddErpOrderSync(Request $request){
        $params = array(
            'order_sn'=>'230620-245791507932225',
            'logistics_id'=>121,
            'order_state'=>1,
            'waybill_no'=>'433297990396999',
        );
        $res = $this->pinDuoDuo->auth_api->request('pdd.erp.order.sync',$params);
        return response()->json(['code'=>0,'msg'=>'success','data'=>$res]);
    }

    /**
     * 查询订单状态
     * @param Request $request
     */
    public function pddOrderstatusGet(Request $request){
        $params = array(
            'order_sns'=>'230620-245791507932225',//订单编号
        );
        $res = $this->pinDuoDuo->auth_api->request('pdd.order.status.get',$params);
        return response()->json(['code'=>0,'msg'=>'success','data'=>$res]);
    }

    /**
     * 编辑商家订单备注
     * @param Request $request
     */
    public function pddOrderNoteUpdate(Request $request){
        $params = array(
            'order_sn'=>'230620-245791507932225',//订单备注订单备注
            'note'=>'',//订单备注
            'tag'=>'',//备注标记：1-红色，2-黄色，3-绿色，4-蓝色，5-紫色，tag与tag_name关联，都入参或都不入参
            'tag_name'=>'',//标记名称；长度最大为3个字符，tag与tag_name关联，都入参或都不入参
        );
        $res = $this->pinDuoDuo->auth_api->request('pdd.order.note.update',$params);
        return response()->json(['code'=>0,'msg'=>'success','data'=>$res]);
    }

    /**
     * 修改订单收件地址接口
     * @param Request $request
     */
    public function pddOrderUpdateAddress(Request $request){
        $params = array(
            'order_sn'=>'230620-245791507932225',//订单编号
            'address'=>'',//收件详细地址
            'city'=>'',//收件地城市
            'city_id'=>'',//城市编码
            'province'=>'',//收件地省份
            'province_id'=>'',//省份编码
            'receiver_name'=>'',//收件人姓名
            'receiver_phone'=>'',//收件人电话，明文
            'town'=>'',//收件地区县
            'town_id'=>'',//区县编码
        );
        $res = $this->pinDuoDuo->auth_api->request('pdd.order.note.update',$params);
        return response()->json(['code'=>0,'msg'=>'success','data'=>$res]);
    }
}
